package test01.demo01;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class MysqlJavaConnection {

    static{
        //1、加载数据驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("驱动加载失败");
        }
    }

    public static void addData(){
        try (
                //2、获取数据库连接，建立两者之间的铁轨
                Connection conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/test01",
                        "root",
                        "root");
                //3.1 创建两者之间的火车,铁轨上的火车
                Statement statement = conn.createStatement();
                ){
            //3.2 定义要执行的sql语句
            String sql = "insert into deptno values (3,'天津')";
            //3.3 通过火车执行sql语句
            statement.execute(sql);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        addData();
    }
}
